package com.guiji.quartz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guiji.common.core.mybatisplus.core.ServicePlusImpl;
import com.guiji.common.core.page.PagePlus;
import com.guiji.common.core.page.TableDataInfo;
import com.guiji.common.utils.PageUtils;
import com.guiji.quartz.mapper.SpiderNewsStatMapper;
import com.guiji.quartz.service.ISpiderNewsStatService;
import com.guiji.quartz.vo.*;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-09-15 17:14
 **/
@Service
@DS("slave")
public class SpiderNewsStatServiceImpl extends ServicePlusImpl<SpiderNewsStatMapper, SpiderNewsStat> implements ISpiderNewsStatService {

	@Override
	public SpiderNewsStatVo queryById(Long id){
		return getVoById(id, SpiderNewsStatVo.class);
	}

	@Override
	public TableDataInfo<SpiderNewsStatVo> queryPageList(SpiderNewsStatQueryBo bo) {
		PagePlus<SpiderNewsStat, SpiderNewsStatVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SpiderNewsStatVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<SpiderNewsStatVo> queryList(SpiderNewsStatQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SpiderNewsStatVo.class);
	}

	private LambdaQueryWrapper<SpiderNewsStat> buildQueryWrapper(SpiderNewsStatQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SpiderNewsStat> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getNewsCount() != null, SpiderNewsStat::getNewsCount, bo.getNewsCount());
		lqw.eq(StrUtil.isNotBlank(bo.getNewsCategory()), SpiderNewsStat::getNewsCategory, bo.getNewsCategory());
		lqw.eq(bo.getStatSign() != null, SpiderNewsStat::getStatSign, bo.getStatSign());
		lqw.eq(StrUtil.isNotBlank(bo.getBackup1()), SpiderNewsStat::getBackup1, bo.getBackup1());
		lqw.eq(StrUtil.isNotBlank(bo.getBackup2()), SpiderNewsStat::getBackup2, bo.getBackup2());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SpiderNewsStatAddBo bo) {
		SpiderNewsStat add = BeanUtil.toBean(bo, SpiderNewsStat.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SpiderNewsStatEditBo bo) {
		SpiderNewsStat update = BeanUtil.toBean(bo, SpiderNewsStat.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SpiderNewsStat entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
